<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
jimport('joomla.application.component.model');  

class WrdModelPcategory extends WrdModel 
{
    var $_data;
	var $_id;
	var $_total = null;
	var $_pagination = null;
	var $_limit = null;
	var $_limitstart = null;

	function __construct(){
		parent::__construct();
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		
		$this->_limit = $mainframe->getUserStateFromRequest( $context.$view.'limit', 'limit', $mainframe->getCfg('list_limit'), 0);
		$this->_limitstart = $mainframe->getUserStateFromRequest( $context.$view.'limitstart', 'limitstart', 0 );

		$this->setState('limit', $this->_limit);
		$this->setState('limitstart', $this->_limitstart);
	}

	function getData(){
		
		if (empty($this->_data)){
			$query = $this->_buildQuery();
			$this->_db->setQuery( $query);
			$this->_data = $this->_db->loadObjectList();
			$categories = array();
			
			if ($search) {
				foreach ($this->_data as $row) {
					$row->treename = $row->title;
					$categories[]=$row;
				}
			}
			else {
				$categories = $this->indentRows($this->_data);
			}
			
			if (isset($categories))
				$total = count($categories);
			else 
				$total = 0;
		
			jimport('joomla.html.pagination');
			$pageNav = new JPagination($total, $this->_limitstart, $this->_limit);
			$categories = @array_slice($categories, $pageNav->limitstart, $pageNav->limit);
			
			return $categories;
		}
	}
	
	function indentRows( & $rows) {

		$children = array ();
		if(count($rows)){
			foreach ($rows as $v) {
				$pt = $v->parent_id;
				$list = @$children[$pt]?$children[$pt]: array ();
				array_push($list, $v);
				$children[$pt] = $list;
			}
		}
		$categories = JHTML::_('menu.treerecurse', 0, '', array (), $children);
		return $categories;
	}

	function getTotal(){
		if (empty($this->_total)){
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}
		return $this->_total;
	}

	function getPagination(){
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->_limitstart, $this->_limit );
		}
		return $this->_pagination;
	}

	function _buildQuery(){
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');

		$orderby 			= $this->_buildContentOrderBy();
		$search 			= $mainframe->getUserStateFromRequest( $context.$view.'search','search','','string');
		$filter_state 		= $mainframe->getUserStateFromRequest( $context.$view.'published','published',-1,'int');
		$filter_user 		= $mainframe->getUserStateFromRequest( $context.$view.'created_id','created_id',-1,'int');
		
		$search 			= JString::strtolower( $search );
		
		$where = array();

		if ( $search ){
			$where[] = 'c.title LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false );
		}
		if ( $filter_state != -1 ){
			$where[] = 'c.published = '.$filter_state;
		}
		if ( $filter_user  != -1){
			$where[] = 'c.created_id = '.$filter_user;
		}
		
		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
		$query = ' SELECT 
					u.name as user_name, 
                    c.id,
                    c.title as title, 
                    c.parent_id,
                    c.desc,
                    c.created_date,  
                    c.published
                  
                	FROM #__wrd_pcategory as c 
                	LEFT JOIN #__users as u ON c.created_id = u.id'

		. $where
		. $orderby;  
		return $query;
	}

	function _buildContentOrderBy(){
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');

		$filter_order     = $mainframe->getUserStateFromRequest( $context.$view.'filter_order_author','filter_order','id' );
		$filter_order_Dir = $mainframe->getUserStateFromRequest( $context.$view.'filter_order_Dir',  'filter_order_Dir', '' );
		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir;
		return $orderby;
	}
    
	function delete($cid = array()){
		$result = false;

		if (count( $cid )){
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid );
			$query = 'DELETE FROM #__wrd_pcategory'
				. ' WHERE id IN ( '.$cids.' )';
			$this->_db->setQuery( $query );
			if(!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}

		return true;
	}
 
	function publish($cid = array(), $publish = 1){
		$user 	=& JFactory::getUser();

		if (count( $cid ))
		{
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid );

			$query = 'UPDATE #__wrd_pcategory '
				. ' SET published = '.(int) $publish
				. ' WHERE id IN ( '.$cids.' )'
			;
			$this->_db->setQuery( $query );
			if (!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}

		return true;
	}
    
     //save to table $nametable
    function store($post,$tablename){
       	$profile = JTable::getInstance($tablename, 'TableWrd');
        $profile->load();
        $profile->bind($post);
        
		if ($profile->store()) {
			return $profile->id;
		} else {
			return 0;
		}
  	}
 
  	function getList(){
  		$query = 'SELECT id AS value, title AS text'
		. ' FROM #__wrd_pcategory WHERE published=1';

		$this->_db->setQuery( $query );
		return $this->_db->loadObjectList();
  	}

	function getUserList(){

		$query = 'SELECT a.created_id as value , b.username as text FROM #__wrd_pcategory a LEFT JOIN #__users b ON a.created_id = b.id 
			GROUP BY a.created_id';
		
		$this->_db->setQuery( $query );
		return $this->_db->loadObjectList();
	}
  	
  	function getItem($id){
  		$query = 'SELECT * FROM #__wrd_pcategory WHERE id = '.$id;
		$this->_db->setQuery($query);
		return $this->_db->loadObject();
  	}
  	
	function getItemName($id=0){
  		if($id!=0){
	  		$query = 'SELECT title FROM #__wrd_pcategory WHERE id = '.$id;
			$this->_db->setQuery($query);
			return $this->_db->loadObject();
  		}else{
  			return NULL;
  		}
  	}
  	
	function getParentItem(){
  		$query = 'SELECT * FROM #__wrd_pcategory WHERE parent_id = 0';
		$this->_db->setQuery($query);
		return $this->_db->loadObject();
  	}
  	
  	function getItemChild($id){
  		$query = 'SELECT * FROM #__wrd_pcategory WHERE parent_id = '.$id;
		$this->_db->setQuery($query);
		return $this->_db->loadObject();
  	}
}